package ch14;

import java.io.File;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Slider;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.stage.Stage;
import javafx.util.Duration;

public class Mediaplayer extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    BorderPane mainpane = new BorderPane();
    Controllers controllers1 = new Controllers();
    File mv = new File("media/sample.mp4");
    Media md = new Media(mv.toURI().toString());
    MediaPlayer mp = new MediaPlayer(md);
    MediaView media = new MediaView(mp);

    @Override
    public void start(Stage primaryStage) throws Exception {
        mainpane.setBottom(controllers1);
        mainpane.setCenter(media);
        Scene scene = new Scene(mainpane, 900, 600);
        primaryStage.setScene(scene);
        primaryStage.show();
        SetControllers();
    }

    void SetControllers() {
        mp.volumeProperty().bind(controllers1.vol.valueProperty().divide(100));
        controllers1.bt1.setOnAction(e -> {
            if (controllers1.bt1.getText().equals("开始")) {
                mp.play();
                controllers1.bt1.setText("暂停");
            } else if (controllers1.bt1.getText().equals("暂停")) {
                mp.pause();
                controllers1.bt1.setText("继续");
            } else {
                mp.play();
                controllers1.bt1.setText("暂停");
            }
        });
        controllers1.bt2.setOnAction(e -> {
            mp.seek(Duration.ZERO);
            mp.pause();
            controllers1.bt1.setText("开始");
        });
        mp.currentTimeProperty().addListener(ov -> {
            controllers1.lb3.setText(computeTime((int) mp.getCurrentTime().toSeconds()));
            controllers1.lb4.setText("/" + computeTime((int) md.getDuration().toSeconds()));
            controllers1.ratio.setValue(mp.getCurrentTime().toSeconds() / md.getDuration().toSeconds() * 100);
        });
        controllers1.ratio.setOnMousePressed(e -> {
            mp.pause();
        });
        controllers1.ratio.setOnMouseDragged(e -> {
            mp.seek(md.getDuration().multiply(controllers1.ratio.getValue() / 100));
        });
        controllers1.ratio.setOnMouseReleased(e -> {
            controllers1.bt1.setText("暂停");
            mp.play();
        });
    }

    static String computeTime(int seconds) {
        String str = "";
        int hour = seconds / 3600;
        int minute = (seconds - 3600 * hour) / 60;
        int second = seconds % 60;
        str += hour + ":" + minute + ":" + second;
        return str;
    }

    class Controllers extends HBox {
        Button bt1 = new Button("开始");
        Button bt2 = new Button("结束");
        Label lb1 = new Label("进度:");
        Slider ratio = new Slider();
        Label lb3 = new Label();
        Label lb4 = new Label();
        Label lb2 = new Label("音量");
        Slider vol = new Slider();

        Controllers() {
            getChildren().clear();
            vol.setValue(50);
            getChildren().addAll(bt1, bt2, lb1, ratio, lb3, lb4, lb2, vol);
        }
    }
}